package org.contextsensitivetodo.context;

import java.util.ArrayList;
import java.util.LinkedList;

import org.contextsensitivetodo.context.configuration.UserContextConfiguration;
import org.contextsensitivetodo.context.rules.location.LocationContextRule;
import org.contextsensitivetodo.context.rules.time.DayOfWeekContextRule;
import org.contextsensitivetodo.context.rules.time.TimeOfTheDayContextRule;
import org.contextsensitivetodo.context.rules.time.TimeRuleTime;

import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

public class ContextDelivery {

	private static ContextDelivery instance = new ContextDelivery();
	
	private LocationManager locationManager;
	
	public LocationManager getLocationManager() {
		return locationManager;
	}


	public void setLocationManager(LocationManager locationManager) {
		this.locationManager = locationManager;
	}


	private ContextDelivery(){
		Log.d("cstodo", "Inserting test data to ContextDelivery");
		
		// fake data for testing:
		
//		UserContext testTimeContext = new UserContext();
//		testTimeContext.setName("Time test context");
//		testTimeContext.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(14, 00), new TimeRuleTime(14, 25)));
//		UserContextConfiguration.getInstance().setupAddNewContext(testTimeContext);
//		
//		
//		
//		
//		UserContext testTimeContext2 = new UserContext();
//		testTimeContext2.setName("Time test context  - 2");
//		testTimeContext2.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(21,48), new TimeRuleTime(22, 25)));
//		UserContextConfiguration.getInstance().setupAddNewContext(testTimeContext2);
//		
//		
//		
//		UserContext testTimeContext3 = new UserContext();
//		testTimeContext3.setName("Time test context - 3");
//		testTimeContext3.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(19,32), new TimeRuleTime(21, 50)));
//		UserContextConfiguration.getInstance().setupAddNewContext(testTimeContext3);
//		
//		
//		UserContext testTimeContext4 = new UserContext();
//		testTimeContext4.setName("Time test context - 4");
//		testTimeContext4.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(0, 00), new TimeRuleTime(22, 00)));
//		UserContextConfiguration.getInstance().setupAddNewContext(testTimeContext4);
//		
		
		
//		Time now = new Time();
//		now.setToNow();
//		int hoursNow = now.hour;
//		int minutesNow = now.minute;
//		
		
//		UserContext testTimeContext5 = new UserContext();
//		testTimeContext5.setName("Time test context - auto: " + hoursNow +":" +(minutesNow + 1) +"-" + hoursNow +":" +(minutesNow + 2));
//		testTimeContext5.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(hoursNow, minutesNow + 1), new TimeRuleTime(hoursNow, minutesNow + 2)));
//		UserContextConfiguration.getInstance().setupAddNewContext(testTimeContext5);
//		
		
		Location home = new Location("none");
		home.setLatitude(48.127488);
		home.setLongitude(11.595970);
		home.setAltitude(500);
		
		Location tvtv = new Location("none");
		tvtv.setLatitude(48.23632);
		tvtv.setLongitude(11.68008);
		tvtv.setAltitude(546);
		
		boolean[] weekdays = {false, true, true, true, true, true, false};
		
		
		
		UserContext testHomeContext = new UserContext();
		testHomeContext.setName("Home");
		testHomeContext.addContextRule(new LocationContextRule(home, 250, false));
		UserContextConfiguration.getInstance().setupAddNewContext(testHomeContext);
		
		UserContext testTvtvContext = new UserContext();
		testTvtvContext.setName("tvtv");
		testTvtvContext.addContextRule(new LocationContextRule(tvtv, 250, false));
		UserContextConfiguration.getInstance().setupAddNewContext(testTvtvContext);
		
		
		UserContext testOutContext = new UserContext();
		testOutContext.setName("out");
		testOutContext.addContextRule(new LocationContextRule(tvtv, 250, true));
		testOutContext.addContextRule(new LocationContextRule(home, 250, true));
		UserContextConfiguration.getInstance().setupAddNewContext(testOutContext);
		
		
		UserContext testOnWayToWorkContext = new UserContext();
		testOnWayToWorkContext.setName("on way to work");
		testOnWayToWorkContext.addContextRule(new LocationContextRule(tvtv, 250, true));
		testOnWayToWorkContext.addContextRule(new LocationContextRule(home, 250, true));
		testOnWayToWorkContext.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(7, 30), new TimeRuleTime(10, 30)));
		testOnWayToWorkContext.addContextRule(new DayOfWeekContextRule(weekdays));
		UserContextConfiguration.getInstance().setupAddNewContext(testOnWayToWorkContext);
		
		UserContext testOnWayToHomeContext = new UserContext();
		testOnWayToHomeContext.setName("on way to home");
		testOnWayToHomeContext.addContextRule(new LocationContextRule(tvtv, 250, true));
		testOnWayToHomeContext.addContextRule(new LocationContextRule(home, 250, true));
		testOnWayToHomeContext.addContextRule(new TimeOfTheDayContextRule(new TimeRuleTime(15, 30), new TimeRuleTime(20, 30)));
		testOnWayToHomeContext.addContextRule(new DayOfWeekContextRule(weekdays));
		UserContextConfiguration.getInstance().setupAddNewContext(testOnWayToHomeContext);
		
		
		
		
		
	};
	
	
	public static ContextDelivery getInstace(){
		return instance;
	}
	
	public ArrayList<UserContext> getContextAsList(){
		LinkedList<UserContext> onContext = UserContextConfiguration.getInstance().getOnContext();
		LinkedList<UserContext> offContext = UserContextConfiguration.getInstance().getOffContext();
	
		
		ArrayList<UserContext> ret = new ArrayList<UserContext>();
		ret.addAll(onContext);
		ret.addAll(offContext);
		return ret;
	}
	
	public String getCurrentContextAsTextForDebug(){
		LinkedList<UserContext> onContext = UserContextConfiguration.getInstance().getOnContext();
		LinkedList<UserContext> offContext = UserContextConfiguration.getInstance().getOffContext();
		
		StringBuffer buff = new StringBuffer();
		
		
		if(!onContext.isEmpty()){
			buff.append("I think you context is:\n");
		}else{
			buff.append("I don't know your context right now :-(\n");
		}
			
		
			
		for (UserContext context : onContext) {
			buff.append(context.getName()+"\n");
		}
	
		
		if(!offContext.isEmpty()){
			buff.append("You have non active context rules:\n");
		}else{
			buff.append("No inactive context rules\n");
		}
			
		
		
		for (UserContext context : offContext) {
			buff.append(context.getName()+"\n");
		}
	
		buff.append("\nLast update was done: "+UserContextConfiguration.getInstance().getLastUpdateTimeStamp());
		
		Log.d("cstodo", "returning context text: " + buff.toString());
		
		return buff.toString();		
	}
	

	
}
